package com.lysj.bhp.borrowing.repository;

import com.lysj.bhp.borrowing.domain.BorrowingRecord;
import com.lysj.bhp.borrowing.vo.BorrowingRecordVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/8/16--9:55
 * @Description: 借贷记录 Jpa
 */
public interface BorrowingRecordRepository extends JpaRepository<BorrowingRecord, Long>, JpaSpecificationExecutor {
    //查找申请中列表需要显示的数据
    List<BorrowingRecord> findByUserIdAndStatusOrStatusOrderByUpdateDateDesc(Long userId, Integer status, Integer status2, Pageable pageable);

    List<BorrowingRecord> findByStatusAndCheckStatusNotInOrderByApplyDateDesc(Integer status, Integer checkStatus);

    BorrowingRecord findByBorrowingRecordIdAndStatus(Long borrowingRecordId, Integer status);

    List<BorrowingRecord> findByStatusInOrderByApplyDateDesc(Integer[] status);

    @Query("SELECT new com.lysj.bhp.borrowing.vo.BorrowingRecordVo(a.borrowingRecordId ,a.borrowingOrderNo ,a.applyDate,a.borrowingType,a.borrowingMoney,a.deadline,a.interest,a.progress,a.defaultStatus,a.mortgageType,a.mortgageMoney,a.checkStatus,a.rateType) from BorrowingRecord a  where  a.status = ?1 AND a.defaultStatus = ?2 AND a.applyDate >= ?3 AND a.applyDate <= ?4 AND a.userId = ?5 And a.borrowingOrderNo like '%'||?6||'%' ")
    Page<BorrowingRecordVo> findBorrowingClosedRecord(Pageable page, Integer status, Integer defaultStatus, Date begin_applyDate, Date end_applyDate, Long userId, String borrowingOrderNo);

    @Query("SELECT new com.lysj.bhp.borrowing.vo.BorrowingRecordVo(a.borrowingRecordId ,a.borrowingOrderNo ,a.applyDate,a.borrowingType,a.borrowingMoney,a.deadline,a.interest,a.progress,a.defaultStatus,a.mortgageType,a.mortgageMoney,a.checkStatus,a.rateType) from BorrowingRecord a  where  a.status = ?1 AND a.applyDate >= ?2 AND a.applyDate <= ?3 AND a.userId = ?4 And a.borrowingOrderNo like '%'||?5||'%'")
    Page<BorrowingRecordVo> findBorrowingAllClosedRecord(Pageable page, Integer status, Date begin_applyDate, Date end_applyDate, Long userId, String borrowingOrderNo);

    @Query("SELECT new com.lysj.bhp.borrowing.vo.BorrowingRecordVo(a.borrowingRecordId ,a.borrowingOrderNo ,a.applyDate,a.borrowingType,a.borrowingMoney,a.deadline,a.interest,a.progress,a.defaultStatus,a.mortgageType,a.mortgageMoney,a.checkStatus,a.rateType) from BorrowingRecord a  where  a.status = ?1 AND a.checkStatus = ?2 AND a.applyDate >= ?3 AND a.applyDate <= ?4 AND a.userId = ?5 And a.borrowingOrderNo like '%'||?6||'%'")
    Page<BorrowingRecordVo> findBorrowingApplyRecord(Pageable page, Integer status, Integer checkStatus, Date begin_applyDate, Date end_applyDate, Long userId, String borrowingOrderNo);

    @Query("SELECT new java.lang.Long(count(a.userId)) from BorrowingRecord a where a.userId = ?1 and a.status in ?2")
    Long BorrowingRecordRow(Long userId, Integer status[]);

    @Query("SELECT new java.lang.Long(count(a.userId)) from BorrowingRecord a where a.userId = ?1 and a.status =1 and a.checkStatus in ?2")
    Long BorrowingRecordApproveRow(Long userId, Integer status[]);

    List<BorrowingRecord> findByStatusNotInOrderByApplyDateDesc(Integer status);

    List<BorrowingRecord> findByUserIdAndBorrowingTypeAndRepayStatusOrderByApplyDateDesc(Long userId, Integer borrowingType, Integer status);

    //测试用户init专用
    List<BorrowingRecord> findByUserId(Long userId);

}
